﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class BrowseByCategory : System.Web.UI.Page
{
    Customer cs;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Customer"] != null)
        {
            cs = (Customer)Session["Customer"];
            string newCust = Request.Params["signup"];

            if (newCust != null && newCust.Equals("true"))
            {
                // this.lblWelcomeText.Visible = true;
            }
        }
        else
        {
            Response.Redirect("home.aspx");
        }

        lblmsg.Text = "";
    }


    protected void btnByDir_Click(object sender, EventArgs e)
    {

        try
        {
            gvGenre.Visible = false;
            gvSearch.Visible = false;
            Director dr = new Director();
            gvDirector.DataSource = dr.getDirector();
            gvDirector.DataBind();
            gvDirector.Visible = true;

        }
        catch (Exception Ex)
        {
            //  lblmsg.Text = "Save Unsuccessful";
        }

    }

    protected void btnByGen_Click(object sender, EventArgs e)
    {

        try
        {
            gvDirector.Visible = false;
            gvSearch.Visible = false;
            Genre gr = new Genre();
            gvGenre.DataSource = gr.getGenre();
            gvGenre.DataBind();
            gvGenre.Visible = true;

        }
        catch (Exception Ex)
        {
            //  lblmsg.Text = "Save Unsuccessful";
        }

    }

    public void gvDirector_RowCommand(Object sender, GridViewCommandEventArgs e)
    {

        // If multiple ButtonField column fields are used, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "Select")
        {

            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Get the last name of the selected author from the appropriate
            // cell in the GridView control.
            GridViewRow selectedRow = gvDirector.Rows[index];

            int index2 = Convert.ToInt32(e.CommandArgument);
            HiddenField myHiddenField = (HiddenField)gvDirector.Rows[index2].Cells[0].FindControl("hdnDirID");

            Movies mv = new Movies();
            gvSearch.DataSource = mv.getMoviesByDirector(Convert.ToInt32(myHiddenField.Value));
            gvSearch.DataBind();
            gvSearch.Visible = true;

            // Save(Convert.ToInt32(myHiddenField.Value), "Search");
        }

    }

    public void gvGenre_RowCommand(Object sender, GridViewCommandEventArgs e)
    {

        // If multiple ButtonField column fields are used, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "Select")
        {

            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Get the last name of the selected author from the appropriate
            // cell in the GridView control.
            GridViewRow selectedRow = gvGenre.Rows[index];

            int index2 = Convert.ToInt32(e.CommandArgument);
            HiddenField myHiddenField = (HiddenField)gvGenre.Rows[index2].Cells[0].FindControl("hdnGenID");

            Movies mv = new Movies();
            gvSearch.DataSource = mv.getMoviesByGenre(Convert.ToInt32(myHiddenField.Value));
            gvSearch.DataBind();
            gvSearch.Visible = true;

            // Save(Convert.ToInt32(myHiddenField.Value), "Search");
        }

    }


    public void gvSearch_RowCommand(Object sender, GridViewCommandEventArgs e)
    {

        // If multiple ButtonField column fields are used, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "New")
        {

            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Get the last name of the selected author from the appropriate
            // cell in the GridView control.
            GridViewRow selectedRow = gvSearch.Rows[index];

            int index2 = Convert.ToInt32(e.CommandArgument);
            HiddenField myHiddenField = (HiddenField)gvSearch.Rows[index2].Cells[0].FindControl("hdnMovieID");

           Save(Convert.ToInt32(myHiddenField.Value), "Search");
        }

    }
    void Save(int movieID, string gridflag)
    {
        //this sub saves data into database
        try
        {
            Movies mv = new Movies();
            int save = mv.saveQueue(cs.Email, movieID);
            if (save == -1)
            {               
               lblmsg.Text = "Currently, this movie is queued/rented out to you";               
            }
            else
            {                
              lblmsg.Text = "Movie added to cart";
                
            }

        }
        catch (Exception Ex)
        {
             lblmsg.Text = "Error: Please contact administrator";
        }

    }

    /// <summary>
    /// gvDirector Gridview pageindexchnaging event 
    /// </summary>
 
    public void gvDirector_PageIndexChanging(Object sender, GridViewPageEventArgs e)
    {
        gvDirector.PageIndex = e.NewPageIndex;
        Director dr = new Director();
        gvDirector.DataSource = dr.getDirector();
        gvDirector.DataBind();
    }

    /// <summary>
    /// gvGenre Gridview pageindexchnaging event 
    /// </summary>
    public void gvGenre_PageIndexChanging(Object sender, GridViewPageEventArgs e)
    {
        gvGenre.PageIndex = e.NewPageIndex;
        Genre dr = new Genre();
        gvGenre.DataSource = dr.getGenre();
        gvGenre.DataBind();
    }
}
